package com.gable.exhibition.thread;

import com.gable.common.util.StringUtil;
import com.gable.exhibition.util.HttpHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mj on 2018/2/28.
 * 安装完成推送短信
 */
public class InstallSendSmsThread implements Runnable{
    Logger logger = LoggerFactory.getLogger(this.getClass());

    private String serviceProviderName;   //服务商名称

    private String deviceName;          //设备名称

    private String installDate;     //安装日期

    private List<String> hospitalMobile;      //医院管理员电话

    private String installCompleteHospitalUrl;  //安装完成推送给医院

    public InstallSendSmsThread(String serviceProviderName,String deviceName,String installDate,
                                List<String> hospitalMobile, String installCompleteHospitalUrl){
        this.serviceProviderName = serviceProviderName;
        this.deviceName = deviceName;
        this.installDate = installDate;
        this.hospitalMobile = hospitalMobile;
        this.installCompleteHospitalUrl = installCompleteHospitalUrl;
    }
    @Override
    public void run() {
        try {
            StringBuilder sb = new StringBuilder();
            for(String phone : hospitalMobile){
                if(StringUtil.isEmpty(phone))
                    continue;
                sb.append(phone).append(",");
            }
            String hospitalMobile = sb.substring(0, sb.length() - 1);
            String message = serviceProviderName+"%&"+deviceName+"%&"+installDate;
            Map<String,String> hospitalMap = new HashMap<String,String>();
            hospitalMap.put("mobile",hospitalMobile);
            hospitalMap.put("message",message);
            String jsonResult = HttpHelper.postRequestResponseBodyAsString(installCompleteHospitalUrl,hospitalMap);
            logger.info("安装完成推送给医院的短信设备名称："+deviceName+",短信发送结果:"+jsonResult);
        } catch (Exception e) {
            logger.info("安装完成推送给医院的短信设备名称："+deviceName+",短信发送失败:"+e);
        }
    }
}
